import os

from utils import doc_util, json_util, read_config, msg_util
from service import data_service

user_path = os.path.join(read_config.data_path, 'common', 'users')
mark_path = os.path.join(read_config.data_path, 'webs', 'mark', 'users')
task_path = os.path.join(read_config.data_path, 'task', 'document', '突破均线')


# 更新标志
def del_flag():
    if os.path.exists(os.path.join(mark_path, 'user_up')):
        os.remove(os.path.join(mark_path, 'user_up'))


# 添加标志
def add_flag():
    doc_util.gen_new_doc(mark_path, 'user_up', [1])


# 更新标志
def update_flag():
    file_list = doc_util.get_file_names(mark_path)
    if len(file_list) == 0:
        return 0
    return 1


# web 页面添加用户
def add_users(fname):
    doc_util.gen_new_doc(mark_path, 'user_up', [1])
    new_fname = os.path.join(user_path, fname.filename)
    fname.save(new_fname)  # 保存文件到指定路径
    return 'success'


# 用户增加监控代码
def add_stock_code(usercode, stockcode):
    msg = {'code': '0000', 'msg': ''}
    if not os.path.exists(os.path.join(user_path, usercode)):
        if msg_util.miao_msg(usercode, '恭喜注册成功') == 0:
            msg['msg'] = '账号无效'
            return msg
        else:
            doc_util.gen_new_doc(user_path, usercode, [])
    s_dict = {}
    stock_list = doc_util.get_path_doc_info(os.path.join(user_path, usercode))
    for stock_info in stock_list:
        stock_info = json_util.info_to_json(stock_info)
        s_dict[stock_info['code']] = stock_info
    if stockcode in s_dict.keys():
        msg['msg'] = '已存在'
    else:
        msg['msg'] = '添加完成'
        stock_list.append(data_service.stock(stockcode))
        doc_util.gen_new_doc(user_path, usercode, stock_list)
        add_flag()  # 增加更新标志
    return msg


def del_stock_code(usercode, stockcode):
    msg = {'code': '0000', 'msg': ''}
    file_names = doc_util.get_file_names(user_path)
    s_dict = {}
    for file in file_names:
        if file == usercode:
            stock_list = doc_util.get_path_doc_info(os.path.join(user_path, file))
            for stock_info in stock_list:
                stock_info = json_util.info_to_json(stock_info)
                s_dict[stock_info['code']] = stock_info
    if stockcode in s_dict.keys():
        msg['msg'] = '已成功'
        s_dict.pop(stockcode)
        doc_util.gen_a_doc(user_path, usercode, s_dict)
        add_flag()  # 增加更新标志
    else:
        msg['msg'] = '不在监控中'
    return msg


# 获取用户所有的监控股票
def get_all_monitor(usercode):
    stock_list = doc_util.get_path_doc_info(os.path.join(user_path, usercode))
    return {usercode: stock_list}


# 获取所有的监控代码
def get_all_stock():
    monitor_dict = doc_util.get_a_doc_dict(os.path.join(task_path, 'stock_virtual_hold'), 'code')
    s_dict = data_service.new_stock_all()
    for monitor in monitor_dict.keys():
        if monitor not in s_dict:
            data_service.stock(monitor)
        else:
            s_dict.pop(monitor)
    return {'监控中': monitor_dict, '未监控': s_dict}


# 删除监控中
def monitor_del(code):
    msg = {'code': '0000', 'msg': '删除成功'}
    monitor_dict = doc_util.get_a_doc_dict(os.path.join(task_path, 'stock_virtual_hold'), 'code')
    if code in monitor_dict.keys():
        monitor_dict.pop(code)
        doc_util.gen_a_doc(task_path, 'stock_virtual_hold', monitor_dict)
        add_flag()
    else:
        msg['msg'] = '已经删除'
    return msg


def monitor_add(code):
    msg = {'code': '0000', 'msg': '添加成功'}
    monitor_dict = doc_util.get_a_doc_dict(os.path.join(task_path, 'stock_virtual_hold'), 'code')
    if code not in monitor_dict.keys():
        monitor_dict[code] = data_service.stock(code)
        doc_util.gen_a_doc(task_path, 'stock_virtual_hold', monitor_dict)
        add_flag()
    else:
        msg['msg'] = '已经监控'
    return msg


if __name__ == '__main__':
    print('********start*******')
    update_flag()
    print('********end*******')
